using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using RPGOnline.Remoting;
using RPGOnline.Remoting.Entities;

namespace RPGOnline.Server
{
    public class ConnectionsManager
    {
        private readonly SessionContext m_sessionContext;
        private Dictionary<Guid, IChatCallback> m_userToCallBackMap = new Dictionary<Guid, IChatCallback>();

        public ConnectionsManager(SessionContext sessionContext)
        {
            m_sessionContext = sessionContext;
        }

        public void AddUser(User user, IChatCallback callbackChannel)
        {
            m_sessionContext.AddUser(user, m_sessionContext.MainChatGroup);
            m_userToCallBackMap.Add(user.Id, callbackChannel);

            foreach (IChatCallback chatCallback in m_userToCallBackMap.Values)
                chatCallback.NewUserConnected(user);

            callbackChannel.UpdatedSession(m_sessionContext);
        }

        public void RemoveUser(User user)
        {
            m_sessionContext.RemoveUser(user);
            m_userToCallBackMap.Remove(user.Id);

            foreach (IChatCallback chatCallback in m_userToCallBackMap.Values)
                chatCallback.UserDisconnected(user);
        }

        public IChatCallback this[User user]
        {
            get { return m_userToCallBackMap[user.Id]; }
        }
    }
}